﻿Imports System
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.Activities
    Friend NotInheritable Class EventDrivenValidator
        Inherits CompositeActivityValidator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim activity As EventDrivenActivity = [TryCast](obj,EventDrivenActivity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(EventDrivenActivity).FullName }), "obj")
            End If
            If ((Not TypeOf activity.Parent Is ListenActivity AndAlso Not TypeOf activity.Parent Is EventHandlersActivity) AndAlso Not TypeOf activity.Parent Is StateActivity) Then
                errors.Add(New ValidationError(SR.GetError_EventDrivenParentNotListen, &H510))
            End If
            Dim errorText As String = String.Empty
            Dim errorNumber As Integer = -1
            Dim activity2 As Activity = If((activity.EnabledActivities.Count > 0), activity.EnabledActivities.Item(0), Nothing)
            If (activity2 Is Nothing) Then
                errorText = SR.GetString("Error_EventDrivenNoFirstActivity")
                errorNumber = &H511
            ElseIf Not TypeOf activity2 Is IEventActivity Then
                errorText = SR.GetError_EventDrivenInvalidFirstActivity
                errorNumber = &H512
            End If
            If (errorText.Length > 0) Then
                errors.Add(New ValidationError(errorText, errorNumber))
            End If
            Return errors
        End Function

        Public Overrides Function ValidateActivityChange(ByVal activity As Activity, ByVal action As ActivityChangeAction) As ValidationError
            If (activity Is Nothing) Then
                Throw New ArgumentNullException("activity")
            End If
            If (action Is Nothing) Then
                Throw New ArgumentNullException("action")
            End If
            Dim action2 As RemovedActivityAction = [TryCast](action,RemovedActivityAction)
            If ((Not action2 Is Nothing) AndAlso (action2.RemovedActivityIndex = 0)) Then
                Return New ValidationError(SR.GetString("Error_EventActivityIsImmutable"), 260, False)
            End If
            Dim action3 As AddedActivityAction = [TryCast](action,AddedActivityAction)
            If ((Not action3 Is Nothing) AndAlso (action3.Index = 0)) Then
                Return New ValidationError(SR.GetString("Error_EventActivityIsImmutable"), 260, False)
            End If
            Return MyBase.ValidateActivityChange(activity, action)
        End Function

    End Class
End Namespace

